﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class CustomerProductListManager : BusinessLogic
    {
        //public ObjectTable GetCourseTable()
        //{
        //    return new ObjectTable(typeof(CUSTOMER_PRODUCT_LISTS));
        //}
        public ObjectTable GetCourseTable(decimal domainID)
        {
            return new ObjectTable(typeof(CUSTOMER_PRODUCT_LISTS), "this.DOMAINID='" + domainID + "'");
        }
        public CUSTOMER_PRODUCT_LISTS Load(int id)
        {
            return (CUSTOMER_PRODUCT_LISTS)ObjectRow.Load(typeof(CUSTOMER_PRODUCT_LISTS), string.Format("this.ID='{0}'", id));
        }

        public CUSTOMER_PRODUCT_LISTS LoadDomianID(string Code, decimal DomainID,decimal ID)
        {
            return (CUSTOMER_PRODUCT_LISTS)ObjectRow.Load(typeof(CUSTOMER_PRODUCT_LISTS), string.Format("this.CODE='{0}' AND this.DOMAINID={1} and this.ID<>'{2}'", Code, DomainID,ID));
        }

        public static void AssignICOProfileProductListItem(bool InsertFlag, int itemID,
                                                    int icoProfileID,
                                                    int productID,
                                                    decimal p_sku,
                                                    string userName,
                                                    decimal domainID)
        {
            SqlData.ExecuteSP("usp_AssignP_SKU",InsertFlag,
                                                itemID,
                                                icoProfileID,
                                                productID,
                                                p_sku,
                                                userName,
                                                domainID);
        }
        

        public static void AssignProductListItem(
                                                    bool InsertFlag,
                                                    int ItemID,
                                                    int CUSTOMER_PRODUCT_LIST_ID,
                                                    int PRODUCT_ID,
                                                    double PIECE_PRICE,
                                                    string REC_USER_CODE,
                                                    DateTime LAST_UPDATE_TIME,
                                                    double CASE_PRICE,
                                                    int FLAG,
                                                    double TPR_DISCOUNT,
                                                    DateTime TPR_START,
                                                    DateTime TPR_END,
            decimal DomainID
                                                    )
        {
            SqlData.ExecuteSP("usp_AssignCUSTOMER_PROD_LIST_ITEMS", InsertFlag ,
                                                                    ItemID,                         
                                                                    CUSTOMER_PRODUCT_LIST_ID ,  
                                                                    PRODUCT_ID ,                
                                                                    PIECE_PRICE ,               
                                                                    REC_USER_CODE ,             
                                                                    LAST_UPDATE_TIME ,            
                                                                    CASE_PRICE ,
                                                                    FLAG,
                                                                    TPR_DISCOUNT, 
                                                                    TPR_START ,        
                                                                    TPR_END,
                                                                    DomainID
                                                                    );
        }

        public int Save(CUSTOMER_PRODUCT_LISTS list)
        {
            if (list != null)
            {
                try
                {
                    if (LoadDomianID(list.CODE, list.DOMAINID,list.ID) != null)
                    {
                        throw new Exception("此代码已存在");
                    }
                   
                    list.Save();
                    return int.Parse(list.ID.ToString());
                }
                catch (Exception err)
                {
                    message = err.Message;throw err;
                }
            }

            return 0;
        }

    

    }
}
